package com.tbsoft.ehr.dao.user;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.tbsoft.ehr.dao.BaseDaoImpl;
import com.tbsoft.ehr.login.RoleInfo;
import com.tbsoft.ehr.login.UserInfo;

/**
 * @desc 用户管理 dao实现
 * @author vidy
 * 
 */
public class UserDaoImpl extends BaseDaoImpl<UserInfo> implements UserDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<UserInfo> getUserList(final UserInfo user,
			final int startIndex, final int count) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer sb = new StringBuffer(
						"from UserInfo usr where usr.status !='D'");
				if (user.getUserId() != null && !user.getUserId().isEmpty()) {
					sb.append(" and usr.userId like '%").append(
							user.getUserId()).append("%'");
				}
				if (user.getUserName() != null && !user.getUserName().isEmpty()) {
					sb.append(" and usr.userName like'%").append(
							user.getUserName()).append("%'");
				}
				Query query = session.createQuery(sb.toString());
				query.setFirstResult(startIndex).setMaxResults(count);
				return query.list();

			}
		});
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int getUserCount(final UserInfo user) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer sb = new StringBuffer(
						"from UserInfo usr where usr.status !='D'");
				if (user.getUserId() != null && !user.getUserId().isEmpty()) {
					sb.append(" and usr.userId like '%").append(
							user.getUserId()).append("%'");
				}
				if (user.getUserName() != null && !user.getUserName().isEmpty()) {
					sb.append(" and usr.userName like'%").append(
							user.getUserName()).append("%'");
				}
				Query query = session.createQuery(sb.toString());
				return query.list();

			}
		});
		return list.size();
	}

	@Override
	public UserInfo getUser(String userId) {
		return (UserInfo) getHibernateTemplate().get(UserInfo.class, userId);
	}

	// 获取加密的密码

	public String getDecodePassword(final String password) {
		Object obj = getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q = session
						.createSQLQuery("select password(?) from dual");
				q.setString(0, password);
				return q.uniqueResult();
			}
		});
		return String.valueOf(obj);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RoleInfo> getAllRoleList() {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hql = "from RoleInfo ri where ri.status != 'D'";
				return session.createQuery(hql).list();
			}
		});
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RoleInfo> getRoleListByIdList(final List<String> idList) {
		if(idList.isEmpty()){
			return new ArrayList<RoleInfo>();
		}
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String strIds = idList.toString().replaceAll("\\[|\\]|\\s", "");
				String hql = "from RoleInfo ri where ri.roleId in (" + strIds
						+ ") and ri.status != 'D'";
				return session.createQuery(hql).list();
			}
		});
		return list;
	}
}
